package org.example;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.example.mapper.StuGradeMapper;

import java.io.IOException;
import java.io.InputStream;

public class MyBatisUtils {

    public static StuGradeMapper getSqlSession() {
        try {
            // 获取SqlSessionFactory
            String resource = "mybatis-config2.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

            // 获取SqlSession对象
            SqlSession sqlSession = sqlSessionFactory.openSession(true);

            //获取Mapper代理
            StuGradeMapper stuGradeMapper = sqlSession.getMapper(StuGradeMapper.class);


            return stuGradeMapper;
        } catch (IOException e) {
            // 处理异常
            e.printStackTrace();
            return null;
        }
    }
}
